package com.uuah.server.management.stats;

import java.io.Serializable;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 *
 * TODO comment ...
 *
 * <p>
 * update record:updator，update time，update content and version
 * </p>
 *
 * @author <a href="jonny_quan@hotmail.com">jonny</a>
 * @date 2009 2009-3-27 下午01:53:22
 * @version 1.0.0
 */
public class UuahServerStats implements Serializable {
	/**
	 *
	 */
	private static final long serialVersionUID = -3909115948881214084L;
	private boolean runStatus;
	private long startTime;
	private long receivedEvent = 0;
	private long sentEvent = 0;
	private long queuedEvent = 0;
	private long maxQueuedEvent = 0;
	private long averageQueueSize = 0;
	private long totalQueuedEvent = 0;
	private long executionError = 0;
	private long fatalError = 0;
	private boolean enabled = false;
	private long statsPeriod = 0;
	private long threadPoolSize = 0;
	private long minaThreadPoolSize = 0;

	private long minaQueuedEvent = 0;
	private int totalMinaQueuedEvent = 0;
	private long maxMinaQueuedEvent = 0;
	private int averageMinaQueueSize = 0;
	private Map<Integer, Long> busiStatsMap = new ConcurrentHashMap();

	public Map<Integer, Long> getBusiStatsMap() {
		return busiStatsMap;
	}

	public void setBusiStatsMap(Map<Integer, Long> busiStatsMap) {
		this.busiStatsMap = busiStatsMap;
	}

	public void addBusiStats(Integer code) {
		Long stat = busiStatsMap.get(code);
		if (stat == null) {
			busiStatsMap.put(code, new Long(0));
		} else {
			stat++;
			busiStatsMap.put(code, new Long(stat));
		}
	}

	public long getMinaQueuedEvent() {
		return minaQueuedEvent;
	}

	public int getTotalMinaQueuedEvent() {
		return totalMinaQueuedEvent;
	}

	public long getMaxMinaQueuedEvent() {
		return maxMinaQueuedEvent;
	}

	public int getAverageMinaQueueSize() {
		return averageMinaQueueSize;
	}

	public long getMaxQueuedEvent() {
		return maxQueuedEvent;
	}

	public long getTotalQueuedEvent() {
		return totalQueuedEvent;
	}

	public long getExecutionError() {
		return executionError;
	}

	public long getFatalError() {
		return fatalError;
	}

	public UuahServerStats() {
		super();
		statsPeriod = System.currentTimeMillis();
	}

	public void setStartTime(long startTime) {
		this.startTime = startTime;
	}

	public boolean isEnabled() {
		return enabled;
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	public void setAverageQueueSize(long averageQueueSize) {
		this.averageQueueSize = averageQueueSize;
	}

	public long getThreadPoolSize() {
		return threadPoolSize;
	}

	public void setThreadPoolSize(long threadPoolSize) {
		this.threadPoolSize = threadPoolSize;
	}

	public long getMinaThreadPoolSize() {
		return minaThreadPoolSize;
	}

	public void setMinaThreadPoolSize(long minaThreadPoolSize) {
		this.minaThreadPoolSize = minaThreadPoolSize;
	}

	public boolean isRunStatus() {
		return runStatus;
	}

	public void setRunStatus(boolean runStatus) {
		this.runStatus = runStatus;
	}

	public long getRunTime() {
		return System.currentTimeMillis() - startTime;
	}

	public long getStartTime() {
		return startTime;
	}

	public synchronized void incReceivedEvent() {
		receivedEvent++;
	}

	public synchronized void incExecutionError() {
		executionError++;
	}

	public synchronized void incFatalError() {
		fatalError++;
	}

	public synchronized void incSentEvent() {
		sentEvent++;
	}

	public synchronized void incQueuedEvent() {
		queuedEvent++;
		totalQueuedEvent++;
		if (queuedEvent > maxQueuedEvent) {
			maxQueuedEvent = queuedEvent;
		}
		averageQueueSize = Math.round(getEventsReceived() / totalQueuedEvent);
	}

	public synchronized void incMinaQueuedEvent() {
		minaQueuedEvent++;
		totalMinaQueuedEvent++;
		if (minaQueuedEvent > maxMinaQueuedEvent) {
			maxMinaQueuedEvent = minaQueuedEvent;
		}
		averageMinaQueueSize = Math.round(getEventsReceived()
				/ totalMinaQueuedEvent);
	}

	public synchronized void decQueuedEvent() {
		if (queuedEvent > 0) {
			queuedEvent--;
		}
	}

	public synchronized void decMinaQueuedEvent() {
		if (minaQueuedEvent > 0) {
			minaQueuedEvent--;
		}
	}

	public synchronized void clear() {
		receivedEvent = 0;
		queuedEvent = 0;
		maxQueuedEvent = 0;
		totalQueuedEvent = 0;
		averageQueueSize = 0;
		sentEvent = 0;
		executionError = 0;
		fatalError = 0;
		minaQueuedEvent = 0;
		totalMinaQueuedEvent = 0;
		maxMinaQueuedEvent = 0;
		averageMinaQueueSize = 0;
		statsPeriod = System.currentTimeMillis();
		busiStatsMap.clear();
	}

	public long getEventsSent() {
		return sentEvent;
	}

	public long getAverageQueueSize() {
		return averageQueueSize;
	}

	public long getMaxQueueSize() {
		return maxQueuedEvent;
	}

	public long getQueuedEvent() {
		return queuedEvent;
	}

	public long getStatsPeriod() {
		return System.currentTimeMillis() - statsPeriod;
	}

	public long getEventsReceived() {
		return receivedEvent;
	}

}
